package com.ltkj.baseplatform.controller;

import com.ltkj.baseplatform.domain.SerCollectData;
import com.ltkj.baseplatform.domain.SerCommentData;
import com.ltkj.baseplatform.domain.SerTravelNotes;
import com.ltkj.baseplatform.domain.SerTravelType;
import com.ltkj.baseplatform.domain.bo.SerTravelNotesBo;
import com.ltkj.baseplatform.domain.vo.SerTravelNotesVo;
import com.ltkj.baseplatform.service.ISerTravelNotesService;
import com.ltkj.baseplatform.service.ISerTravelTypeService;
import com.ltkj.common.core.exception.ServiceException;
import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.core.web.page.TableDataInfo;
import com.ltkj.common.log.annotation.Log;
import com.ltkj.common.log.enums.BusinessType;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.system.api.RemoteFileService;
import com.ltkj.system.api.domain.CommAttachment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序游记管理Controller
 * 和后台管理程序分开
 * 
 * @author liupf
 * @date 2023-09-04
 */
@Api(tags = "小程序游记管理")
@RestController
@RequestMapping("/applet/notes")
public class AppletTravelNotesController extends BaseController
{
    @Autowired
    private ISerTravelNotesService serTravelNotesService;

    @Autowired
    private ISerTravelTypeService serTravelTypeService;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询游记管理列表
     */
    @ApiOperation("查询游记列表")
    @GetMapping("/list")
    public TableDataInfo list(SerTravelNotes serTravelNotes)
    {
        startPage();
        List<SerTravelNotesVo> list = serTravelNotesService.selectAppletTravelNotesList(serTravelNotes);
        return getDataTable(list);
    }

    /**
     * 查询游记管理列表
     */
    //@ApiOperation("查询我的发布游记列表")
    @GetMapping("/publish/list")
    public TableDataInfo myPublishList(SerTravelNotes serTravelNotes)
    {
        serTravelNotes.setUserId(SecurityUtils.getUserId());
        startPage();
        List<SerTravelNotesVo> list = serTravelNotesService.selectAppletTravelNotesList(serTravelNotes);
        return getDataTable(list);
    }

    /**
     * 获取游记管理详细信息
     */
    @ApiOperation("获取游记详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        serTravelNotesService.updateViewNum(id);
        return success(serTravelNotesService.selectAppletTravelNotesById(id));
    }

    /**
     * 发布游记
     */
    @ApiOperation("发布游记")
    @Log(title = "小程序游记管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SerTravelNotesBo serTravelNotesBo)
    {
        return toAjax(serTravelNotesService.insertAppletTravelNotes(serTravelNotesBo));
    }

    /**
     * 修改游记
     */
    @ApiOperation("修改游记")
    @Log(title = "小程序游记管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SerTravelNotesBo serTravelNotesBo)
    {
        return toAjax(serTravelNotesService.updateAppletTravelNotes(serTravelNotesBo));
    }

    /**
     * 删除游记管理
     */
    @ApiOperation("删除游记")
    @Log(title = "小程序游记管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(serTravelNotesService.deleteSerTravelNotesByIds(ids));
    }

    /**
     * 获取游记分类列表
     */
    @ApiOperation("获取游记分类列表")
    @GetMapping(value = "/type/list")
    public AjaxResult getTypeList() {
        return success(serTravelTypeService.selectSerTravelTypeList(new SerTravelType()));
    }

    /**
     * 点赞接口
     */
    @GetMapping("/likes")
    @ApiOperation("点赞")
    public AjaxResult addLikes(@RequestParam("id") Long id)
    {
        return toAjax(serTravelNotesService.addLikes(id));
    }

    /**
     * 取消点赞
     */
    @GetMapping("/cancel/likes")
    @ApiOperation("取消点赞")
    public AjaxResult cancelLikes(@RequestParam("id") Long id)
    {
        SerTravelNotes serTravelNotes = serTravelNotesService.selectSerTravelNotesById(id);
        if(serTravelNotes == null){
            AjaxResult.error("游记数据不存在！");
        }
        //如果点赞数为0还要取消点赞，就返回操作成功（理论上只会在联调阶段出现该问题）
        if(serTravelNotes.getLikes() == null || serTravelNotes.getLikes().equals(0L)){
            AjaxResult.success();
        }
        return toAjax(serTravelNotesService.cancelLikes(id));
    }

    /**
     * 收藏接口
     */
    @PostMapping("/collect")
    @ApiOperation("收藏")
    public AjaxResult addCollect(@RequestBody SerCollectData serCollectData)
    {
        if(serCollectData.getBizId() == null){
            AjaxResult.error("请输入业务编号！");
        }
        SerTravelNotes serTravelNotes = serTravelNotesService.selectSerTravelNotesById(serCollectData.getBizId());
        if(serTravelNotes == null){
            AjaxResult.error("游记数据不存在！");
        }
        //查询游记中的文件
        List<CommAttachment> attachments = remoteFileService.selectAtta(serCollectData.getBizId(), "travel_notes-file");

        if(!CollectionUtils.isEmpty(attachments) && attachments.size() > 0){
            //取第一张图片展示
            CommAttachment commAttachment = attachments.get(0);
            //设置封面图片路径
            serCollectData.setImgPath(StringUtils.hasText(commAttachment.getAttaZoomPath()) ? commAttachment.getAttaZoomPath() : commAttachment.getAttaPath());
        }
        return toAjax(serTravelNotesService.addCollect(serCollectData,serTravelNotes));
    }

    /**
     * 取消收藏
     */
    @GetMapping("/cancel/colect")
    @ApiOperation("取消收藏")
    public AjaxResult cancelColect(@RequestParam("id") Long id)
    {
        SerTravelNotes serTravelNotes = serTravelNotesService.selectSerTravelNotesById(id);
        if(serTravelNotes == null){
            AjaxResult.error("游记数据不存在！");
        }
        //如果收藏数为0还要取消收藏，就返回操作成功（理论上只会在联调阶段出现该问题）
        if(serTravelNotes.getCollectNum() == null || serTravelNotes.getCollectNum().equals(0L)){
            AjaxResult.success();
        }
        return toAjax(serTravelNotesService.cancelColect(id));
    }

    /**
     * 发布评论接口
     */
    @PostMapping("/publish/comment")
    @ApiOperation("发布评论")
    public AjaxResult publishComment(@RequestBody SerCommentData serCommentData)
    {
        return toAjax(serTravelNotesService.publishComment(serCommentData));
    }

    /**
     * 删除评论
     */
    @GetMapping("/delete/comment")
    @ApiOperation("删除评论")
    public AjaxResult deleteComment(@RequestParam("id") Long id)
    {
        return toAjax(serTravelNotesService.deleteComment(id));
    }
}
